﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace Ession
{
    /// <summary>
    ///     用于评论优酷
    /// </summary>
    /// <remarks>
    ///     
    /// </remarks>
    public class YoukuCommentor
    {
        public string CommentURL { get; set; }

        public List<string> CommentContent { get; set; }

        public YoukuCommentor()
        {
            CommentContent = new List<string>()
                {
                   "very nice!", "nice","Good","very good!","excellent","very very nice",
                   "来个鼠标","鼠标~~~"
                };
            CommentURL = "";
            Interval = 60000;
            PageSize = 30;
            ap = new AutoPost();

        }

        public int Interval { get; set; }
        private AutoPost ap;
        public void Start()
        {

        }
        public void Start(WebBrowser browser, String CommentURL)
        {
            WebBrowser = browser;
            this.CommentURL = CommentURL;
            browser.Navigate(CommentURL);
            browser.Navigated += browser_Navigated;

        }

        public WebBrowser WebBrowser { get; set; }
        private bool hasInvoke = false;
        void browser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {


        }

        public int PageSize { get; set; }
        private int lastpage = -1;
        private int maxOnePage = 3;//每页最多三条
        private int commentIndex = 4;
        Random r = new Random();
        public void Comment()
        {
            try
            {
                if (WebBrowser.ReadyState == WebBrowserReadyState.Complete)
                {
                    if (WebBrowser.Document != null)
                    {
                        var elementById = WebBrowser.Document.GetElementById("content");
                        if (elementById != null)
                        {
                            var pageCount = GetTotalCommentsCount() / PageSize;
                            //开始刷
                            string con = CommentContent[r.Next(CommentContent.Count - 1)];
                            var app = r.Next(10);
                            for (int i = 0; i < app; i++)
                            {
                                con += "~";
                            }

                            elementById.InnerText = con;
                            commentIndex++;
                            if (lastpage == -1 || pageCount != lastpage || maxOnePage > 0)//一页
                            {
                                if (pageCount != lastpage)
                                {
                                    maxOnePage = 3;
                                }
                                try
                                {
                                    WebBrowser.Document.InvokeScript("commentNew",
                                                     new object[]
                                                         {
                                                             "", "", "", false
                                                         });
                                }
                                catch (Exception)
                                { 
                                    
                                }
                              
                                maxOnePage--;
                                lastpage = pageCount;
                            }

                        }
                    }
                }
            }
            catch (Exception)
            {
                
                
            }
          

        }

        private System.Threading.Timer timer;
        string reg = "全部评论.*?\\(((\\d|,)+)\\)";

        int GetTotalCommentsCount()
        {
            try
            {
                WebBrowser.Document.InvokeScript("loadCommentNew");
                Regex r = new Regex(reg);
                var m = r.Match(WebBrowser.Document.Body.InnerHtml);
                return int.Parse(m.Groups[1].ToString().Replace(",", ""));

            }
            catch (Exception ex)
            {
                return -1;

            }

        }
    }
}
